package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Consumer;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;


    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //添加套餐表
        this.save(setmealDto);
        //添加套餐菜品表
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();
        dishList.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(dishList);
    }

    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = Wrappers.lambdaQuery(Setmeal.class)
                .in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, 1);
        //查询启用套餐的数量
        int count = this.count(wrapper);
        //启用套餐数量抛出异常，回响不能删除
        if (count > 0) {
            throw new CustomerException("有套餐正在售卖，不能删除");
        }
        //根据ID删除
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> queryWrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(queryWrapper);
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //根据ID查询
        Setmeal setmeal = this.getById(id);
        //条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .eq(SetmealDish::getSetmealId, id);
        //根据条件查询
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        SetmealDto setmealDto = new SetmealDto();
        //拷贝属性
        BeanUtils.copyProperties(setmeal, setmealDto);

        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐
        this.updateById(setmealDto);
        //获取套餐ID
        Long id = setmealDto.getId();
        //条件构造器
        LambdaQueryWrapper<SetmealDish> wrapper = Wrappers.lambdaQuery(SetmealDish.class).eq(SetmealDish::getSetmealId, id);
       //删除套餐菜品
        setmealDishService.remove(wrapper);
        //获取浏览器套餐菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //添加套餐ID到套餐菜品表中
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
        //添加套餐菜品
        setmealDishService.saveBatch(setmealDishes);
    }
}
